<?php 
class User_manage extends MY_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model('user_model', '', true);
		$this->load->model('society_model', '', true);
		$this->load->model('association_model', '', true);
		$this->load->model('teacher_model', '', true);
		$this->load->model('admin_model', '', true);
	}
	
	function user_list() {
		$keywords = urldecode($this->input->post("key_words"));
		$offset = $this->input->post("offset");
		if ($keywords != '') {
        	$condition['keywords'] = $keywords;
        }
		if ($offset != '') {
			$condition['offset'] = $offset;
		} else {
			$condition['offset'] = 0;
		}
		
		switch ($_SESSION['cid']) {
			case ADMIN_SUPER_CID:
				$user_category = $this->input->post("user_category");
				if ($user_category != 0) {
		        	$condition['category'] = $user_category;
		        }
		        
		        $data['user_info'] = $this->user_model->search_by_condition($condition);
		        $data['user_num'] = $this->user_model->search_by_condition($condition, "", TRUE);
		        $data['keywords'] = $keywords;
		        $data['user_category'] = $user_category;
				$this->load->view('admin/user_manage/main', $data);
				break;
			case ADMIN_TEACHER_CID:
				$user_category = $this->input->post("user_category");
				$confirm_FLAG = $this->input->post("confirm_FLAG");
				
				if ($user_category != "") {
		        	$condition['category'] = $user_category;
		        }
				if ($confirm_FLAG != "") {
		        	$condition['confirm_FLAG'] = $confirm_FLAG;
		        }
				
				$data['user_info'] = $this->society_model->search_by_condition($condition);
		        $data['user_num'] = $this->society_model->search_by_condition($condition, "", TRUE);
		        $data['keywords'] = $keywords;
		        $data['user_category'] = $user_category;
		        $data['confirm_FLAG'] = $confirm_FLAG;
				$this->load->view('admin/user_manage/teacher_main', $data);
				break;
			default:
				$function_FLAG = $this->input->post("function_FLAG");
				$confirm_FLAG = $this->input->post("confirm_FLAG");
				
				if ($function_FLAG != "") {
		        	$condition['function_FLAG'] = $function_FLAG;
		        }
				if ($confirm_FLAG != "") {
		        	$condition['confirm_FLAG'] = $confirm_FLAG;
		        	$condition['function_FLAG'] = NOT_FUNCTION;
		        }
				$condition['category'] = $_SESSION['cid'];
		        
				$data['user_info'] = $this->association_model->search_by_condition($condition);
		        $data['user_num'] = $this->association_model->search_by_condition($condition, "", TRUE);
		        $data['keywords'] = $keywords;
		        $data['function_FLAG'] = $function_FLAG;
		        $data['confirm_FLAG'] = $confirm_FLAG;
				$this->load->view('admin/user_manage/association_main', $data);
		}
	}
	
	function user_modify() {
		$user_id = $this->input->post("id");
		if ($user_id == $_SESSION['id']) { return FALSE; }
		
		switch ($_SESSION['cid']) {
			case ADMIN_SUPER_CID:
				$data = $this->user_model->get_by_id($user_id);
				$this->load->view('admin/user_manage/modify', $data);
				break;
			case ADMIN_TEACHER_CID:
				$this->load->library('col_pro_data');
				$data['user_data'] = $this->user_model->get_by_id($user_id);
				$data['society_data'] = $this->society_model->_get_by_foreign_id($user_id);
				$data['teacher_data'] = $this->teacher_model->_get_by_foreign_id($data['society_data']['s_teacher']);
				$this->load->view('admin/user_manage/teacher_modify', $data);
				break;
			default:
				$this->load->library('col_pro_data');
				$data['user_data'] = $this->user_model->get_by_id($user_id);
				$data['association_data'] = $this->association_model->_get_by_foreign_id($user_id);
				$this->load->view('admin/user_manage/association_modify', $data);
		}
	}
	
	function user_modify_submit() {
		$user_id = $this->input->post("id");
		if ($user_id == $_SESSION['id']) { return FALSE; }
		
		$password = $this->input->post("password");
		$email = $this->input->post("email");
		
		$data['id'] = $user_id;
		if ($password != '') {
			$data['password'] = $password;
		}
		$data['email'] = $email;
		
        $this->user_model->update($data);
	}
	
	function user_delete() {
		$user_id = $this->input->post("id");
        if ($user_id == $_SESSION['id']) { return FALSE; }
        
        switch ($_SESSION['cid']) {
			case ADMIN_SUPER_CID:
	        	$ucid = $this->user_model->get_by_id($user_id, 'category_id');
				switch ($ucid) {
					case SOCIETY_UCID:
						$this->user_model->delete($user_id);
						$teacher_id = $this->society_model->_get_by_foreign_id($user_id, 's_teacher');
						$this->society_model->delete_by_foreign_id($user_id);
						
						$this->user_model->delete($teacher_id);
						$this->teacher_model->delete_by_foreign_id($teacher_id);
						
						$this->load->model('news_model', '', true);
						$this->load->model('announcement_model', '', true);
						$this->news_model->_delete_by_specified_key('author_id', $user_id);
						$this->announcement_model->_delete_by_specified_key('author_id', $user_id);
						break;
					case ASSOCIATION_UCID:
						$this->user_model->delete($user_id);
						
						$this->association_model->delete_by_foreign_id($user_id);
						
						$this->load->model('news_model', '', true);
		        		$this->load->model('announcement_model', '', true);
		        		$update_data['poster_id'] = NULL;
						$this->news_model->_update_by_specified_key('poster_id', $user_id, $update_data);
						$this->announcement_model->_update_by_specified_key('poster_id', $user_id, $update_data);
						break;
					case TEACHER_UCID:
						return FALSE;
						break;
					case ADMIN_UCID:
						$this->user_model->delete($user_id);
						$this->admin_model->delete_by_foreign_id($user_id);
						break;
				}
				break;
			case ADMIN_TEACHER_CID:
				return FALSE;
				break;
			default:
				$this->user_model->delete($user_id);
        		$this->association_model->delete_by_foreign_id($user_id);
        		
        		$this->load->model('news_model', '', true);
        		$this->load->model('announcement_model', '', true);
        		$update_data['poster_id'] = NULL;
				$this->news_model->_update_by_specified_key('poster_id', $user_id, $update_data);
				$this->announcement_model->_update_by_specified_key('poster_id', $user_id, $update_data);
		}
	}
	
	function user_create() {
		$user_name = $this->input->post("user_name");
		$password = $this->input->post("password");
		$category_id = $this->input->post("category_id");
		
		$validate_data['name'] = $user_name;
		$validate_data['password'] = $password;
	    if (!$this->_ajax_validate($validate_data)) { return ; }
		
		switch ($_SESSION['cid']) {
			case ADMIN_SUPER_CID:
				if ($category_id == '') {
					$this->output->set_output(json_encode('null_admin_category_error'));
					return FALSE;
				}
			case ADMIN_TEACHER_CID:
				//return FALSE;
				break;
			default:
				//return FALSE;
		}
	    
		if (!$this->user_model->get_user_by_name_single($user_name)) {
			$user_data['name'] = $user_name;
			$user_data['password'] = $password;
			$user_data['create_time'] = time();
			
			switch ($_SESSION['cid']) {
				case ADMIN_SUPER_CID:
					$user_data['category_id'] = ADMIN_UCID;
					$user_id = $this->user_model->save($user_data);
					
					$admin_data['admin_id'] = $user_id;
					$admin_data['category_id'] = $category_id;
					$this->admin_model->save($admin_data);
					break;
				case ADMIN_TEACHER_CID:
					return FALSE;
					break;
				default:
					$user_data['category_id'] = ASSOCIATION_UCID;
					$user_id = $this->user_model->save($user_data);
					
					$association_data['employee_id'] = $user_id;
					$association_data['a_category'] = $_SESSION['cid'];
					$association_data['confirm_FLAG'] = ASSOCIATION_VERIFY_PASS;
					$association_data['function_FLAG'] = IS_FUNCTION;
					$this->association_model->save($association_data);
			}
			
			$this->output->set_output(json_encode('succeed'));
		} else {
			$this->output->set_output(json_encode('username_duplicate_error'));
		}
        
	}
	
	function user_verify_submit() {
		$user_id = $this->input->post("id");
		if ($user_id == $_SESSION['id']) { return FALSE; }
		
		$confirm_FLAG = $this->input->post("confirm_FLAG");
		
		switch ($_SESSION['cid']) {
			case ADMIN_SUPER_CID:
				return FALSE;
				break;
			case ADMIN_TEACHER_CID:
				$data['confirm_FLAG'] = $confirm_FLAG;
				$this->society_model->update_by_foreign_id($user_id, $data);
				break;
			default:
				$data['confirm_FLAG'] = $confirm_FLAG;
				$this->association_model->update_by_foreign_id($user_id, $data);
		}
	}
}